<extend name="admin/main"/>

<block name="title">
    <title>水印管理</title>
</block>

<block name="css"></block>

<block name="heading">水印管理</block>

<block name="content">
    <div class="col-xs-4" data-type="ANTI_FAKE_IMAGE">
        <p>图片水印</p>
        <div style="min-height: 100px;">
            <img src="{$image.value}" width="100%">
        </div>
        <div class="btn-group">
            <button class="btn btn-default edit-image">编辑</button>
            <button class="btn btn-default e-a">{$image['is_enable']==1?'启用':'禁用'}</button>
        </div>
    </div>
    <div class="col-xs-4" data-type="ANTI_FAKE_TEXT">
        <p>文字水印</p>
        <div style="min-height: 100px">
            <h2>{$text.value}</h2>
        </div>
        <div class="btn-group">
            <button class="btn btn-default edit-text">编辑</button>
            <button class="btn btn-default e-a">{$text['is_enable']==1?'启用':'禁用'}</button>
        </div>
    </div>
</block>

<block name="js">
    <script type="text/javascript" src="__PUBLIC__/assets/demo/demo.js"></script>
    <script type="text/javascript" src="__PUBLIC__/assets/demo/demo-switcher.js"></script>
    <script type="text/javascript" src="__PUBLIC__/assets/plugins/bootbox/bootbox.js"></script>
    <script>
        $(document).ready(function () {
            $('.edit-image').click(function () {
                bootbox.confirm('<div class="form-group">' +
                    '<label>选择图片:</label>' +
                    '<input type="file" id="image-a" class="form-control">' +
                    '</div>', function (res) {
                    if (res === false) return;
                    var data = new FormData();
                    data.append('file', $('#image-a')[0].files[0]);
                    axios.post('/admin/webSetting/updateAntiFakeImage',
                        data, {headers: {'Content-Type': 'application/x-www-form-urlencoded'}}).then(function (res) {
                        message(res.data);
                    });
                });
            });
            $('.edit-text').click(function () {
                bootbox.prompt("输入文字", function (result) {
                    if (result === null) {
                        bootbox.alert("Prompt dismissed");
                    } else {
                        $.post('/admin/webSetting/updateAntiFakeText', {text: result}, function (res) {
                            message(JSON.parse(res));
                        });
                    }
                });
            });
            $('.e-a').click(function () {
                var type = $(this).parent().parent().attr('data-type');
                $.post('/admin/WebSetting/switchAntiFakeIsEnable', {key: type}, function (res) {
                    message(JSON.parse(res));
                });
            })
        });
        function message(data) {
            if (data.success === false) {
                new PNotify({
                    title: 'Oh On!',
                    text: JSON.stringify(data.data),
                    type: 'error',
                    icon: 'ti ti-info-alt',
                    styling: 'fontawesome'
                });
            } else {
                window.location.reload();
            }
        }
    </script>
</block>
